<?php

/*
 *
 *
 */
error_reporting(E_ALL);
ob_implicit_flush(true);
require_once(dirname(__FILE__) . '/../include.all.php');

$time = time();

$def0 = new Deferred();
setTimeout(function() use($def0) {
    global $time;
    echo "def0: " . (time() - $time) . "\n";
    $def0->callback('a');
}, 1);
$def1 = new Deferred();
setTimeout(function() use($def1) {
    global $time;
    echo "def1: " . (time() - $time) . "\n";
    $def1->callback('b');
}, 2);
$def2 = new Deferred();
setTimeout(function() use($def2) {
    global $time;
    echo "def2: " . (time() - $time) . "\n";
    $def20 = new Deferred();
    setTimeout(function() use($def20) {
        global $time;
        echo "def20: " . (time() - $time) . "\n";
        $def20->callback('c');
    }, 1);
    $def21 = new Deferred();
    setTimeout(function() use($def21) {
        global $time;
        echo "def21: " . (time() - $time) . "\n";
        $def21->callback('d');
    }, 1);
        $def22 = new Deferred();
        $def22->callback('e');
    
        $list = new DeferredList(array($def20, $def21, $def22));
        $def2->callback($list);
}, 3);

$deflist = new DeferredList(array($def0, $def1, $def2));
$deflist->addCallback(function($defs) {
    global $time;
    echo "All defs: "     . (time() - $time) . "\n";
    echo '$defs[0] = '    . $defs[0] . "\n";
    echo '$defs[1] = '    . $defs[1] . "\n";
    echo '$defs[2][0] = ' . $defs[2][0] . "\n";
    echo '$defs[2][1] = ' . $defs[2][1] . "\n";
    echo '$defs[2][2] = ' . $defs[2][2] . "\n";
});




echo "\nAll Init\n";
Tick::getInstance()->startExecution();
echo "\nAll Ended\n";